package com.hjq.demo.app.vb

import androidx.viewbinding.ViewBinding
import com.hjq.base.BaseVBActivity
import com.hjq.demo.action.ToastAction
import com.hjq.demo.action.TitleBarAction
import com.hjq.http.listener.OnHttpListener
import com.hjq.bar.TitleBar
import com.gyf.immersionbar.ImmersionBar
import androidx.annotation.StringRes
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.hjq.base.BaseDialog
import com.hjq.demo.R
import com.hjq.demo.http.model.HttpData
import com.hjq.demo.ui.dialog.WaitDialog
import okhttp3.Call
import java.lang.Exception

/**
 *
 * @author he
 * 创建日期 2021/7/1
 * 描述 viewbinding 基类
 */
abstract class AppVbActivity<T : ViewBinding> : BaseVBActivity<T>(), ToastAction, TitleBarAction,
    OnHttpListener<Any?> {
    /** 标题栏对象  */
    private var mTitleBar: TitleBar? = null

    /** 状态栏沉浸 */
    private var immersionBar: ImmersionBar? = null

    /** 加载对话框  */
    private var dialog: BaseDialog? = null

    /** 对话框数量  */
    private var mDialogCount = 0

    /**
     * 当前加载对话框是否在显示中
     */
    open fun isShowDialog(): Boolean {
        return dialog != null && dialog!!.isShowing
    }

    /**
     * 显示加载对话框
     */
    fun showDialog() {
        if (isFinishing || isDestroyed) {
            return
        }
        mDialogCount++
        postDelayed({
            if (mDialogCount <= 0 || isFinishing || isDestroyed) {
                return@postDelayed
            }
            if (dialog == null) {
                dialog = WaitDialog.Builder(this)
                    .setCancelable(false)
                    .create()
            }
            if (!dialog!!.isShowing) {
                dialog!!.show()
            }
        }, 300)
    }

    /**
     * 隐藏加载对话框
     */
    fun hideDialog() {
        if (isFinishing || isDestroyed) {
            return
        }
        if (mDialogCount > 0) {
            mDialogCount--
        }
        if (mDialogCount != 0 || dialog == null || !dialog!!.isShowing) {
            return
        }
        dialog!!.dismiss()
    }

    override fun initLayout() {
        super.initLayout()
        if (getTitleBar() != null) {
            getTitleBar()!!.setOnTitleBarListener(this)
        }

        // 初始化沉浸式状态栏
        if (isStatusBarEnabled()) {
            getStatusBarConfig().init()

            // 设置标题栏沉浸
            if (getTitleBar() != null) {
                ImmersionBar.setTitleBar(this, getTitleBar())
            }
        }
    }

    /**
     * 是否使用沉浸式状态栏
     */
    protected open fun isStatusBarEnabled(): Boolean {
        return true
    }

    /**
     * 状态栏字体深色模式
     */
    open fun isStatusBarDarkFont(): Boolean {
        return true
    }

    /**
     * 获取状态栏沉浸的配置对象
     */
    open fun getStatusBarConfig(): ImmersionBar {
        if (immersionBar == null) {
            immersionBar = createStatusBarConfig()
        }
        return immersionBar!!
    }

    /**
     * 初始化沉浸式状态栏
     */
    protected open fun createStatusBarConfig(): ImmersionBar {
        return ImmersionBar.with(this) // 默认状态栏字体颜色为黑色
            .statusBarDarkFont(isStatusBarDarkFont()) // 指定导航栏背景颜色
            .navigationBarColor(android.R.color.white) // 状态栏字体和导航栏内容自动变色，必须指定状态栏颜色和导航栏颜色才可以自动变色
            .autoDarkModeEnable(true, 0.2f)
    }

    /**
     * 设置标题栏的标题
     */
    override fun setTitle(@StringRes id: Int) {
        setTitle(getString(id))
    }

    /**
     * 设置标题栏的标题
     */
    override fun setTitle(title: CharSequence?) {
        super<BaseVBActivity>.setTitle(title)
        if (getTitleBar() != null) {
            getTitleBar()!!.title = title
        }
    }

    override fun getTitleBar(): TitleBar? {
        if (mTitleBar == null) {
            mTitleBar = obtainTitleBar(getContentView())
        }
        return mTitleBar
    }

    override fun onLeftClick(view: View) {
        onBackPressed()
    }

    override fun startActivityForResult(intent: Intent, requestCode: Int, options: Bundle?) {
        super.startActivityForResult(intent, requestCode, options)
        overridePendingTransition(R.anim.right_in_activity, R.anim.right_out_activity)
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(R.anim.left_in_activity, R.anim.left_out_activity)
    }

    /**
     * [OnHttpListener]
     */
    override fun onStart(call: Call) {
        showDialog()
    }

    override fun onSucceed(result: Any?) {
        if (result is HttpData<*>) {
            toast(result.getMessage())
        }
    }

    override fun onFail(e: Exception) {
        toast(e.message)
    }

    override fun onEnd(call: Call) {
        hideDialog()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (isShowDialog()) {
            hideDialog()
        }
        dialog = null
    }
}